<!DOCTYPE html>
<title>Tests that the user's preferred languages are honored.</title>
<video></video>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
async_test(function(t) {
    var video = document.querySelector("video");
    t.add_cleanup(function() {
        internals.setUserPreferredLanguages([]);
        internals.settings.setTextTrackKindUserPreference("default");
    });

    // Set user preferred languages.
    internals.setUserPreferredLanguages(['jp', 'es-ES', 'en', 'fr']);
    internals.settings.setTextTrackKindUserPreference('captions');

    var timer;
    var expectedLanguage;
    var testList =
    {
        currentTestIndex : -1,
        tests :
        [
            {
                // A track language matches one of the user's preferred languages exactly.
                languages : ['fr', 'en', 'jp'],
                expectedLanguage : "jp"
            },
            {
                // A track language without locale exactly matches
                // one of the user's preferred languages.
                languages : ['fr-CH', 'da'],
                expectedLanguage : "fr-CH"
            },
            {
                // A track language without locale matches one of
                // the user's preferred languages without locale.
                languages : ['fr', 'es-MX'],
                expectedLanguage : "es-MX"
            },
            {
                // No track language matches any of the user's preferred languages.
                languages : ['fa', 'ru', 'no'],
                expectedLanguage : "fa"
            }
        ]
    };

    runNextTest();

    function runNextTest() {
        testList.currentTestIndex++;
        if (testList.currentTestIndex >= testList.tests.length) {
            var tracks = document.querySelectorAll('track');
            assert_equals(video.textTracks.length, tracks.length);
            t.done();
            return;
        }

        createTrackElements(testList.tests[testList.currentTestIndex].languages);
    }

    function createTrackElements(languages) {
        var tracks = document.querySelectorAll('track');
        for (var index = 0; index < tracks.length; ++index)
            video.removeChild(tracks[index]);

        for (var index = 0; index < languages.length; ++index)
            createTrackElement(languages[index], "captions-webvtt/tc004-webvtt-file.vtt");
    }

    function createTrackElement(language, src) {
        var track = document.createElement('track');
        track.setAttribute('kind', "captions");
        track.setAttribute('src', src);
        track.setAttribute('srclang', language);
        track.onload = t.step_func(trackLoaded);
        video.appendChild(track);
    }

    function trackLoaded() {
        var track = event.target;
        assert_equals(track.readyState, HTMLTrackElement.LOADED);
        assert_equals(track.srclang, testList.tests[testList.currentTestIndex].expectedLanguage);

        timer = setTimeout(runNextTest, 200);
    }
});
</script>
